package com.iwomy.secureplat.platform.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.iwomy.secureplat.common.data.datascope.DataScope;
import com.iwomy.secureplat.common.security.util.SecurityUtils;
import com.iwomy.secureplat.platform.dto.BasicOperatorTypeDTO;
import com.iwomy.secureplat.platform.entity.BasicAuthorizationRecordEntity;
import com.iwomy.secureplat.platform.entity.BasicOperatorTypeEntity;
import com.iwomy.secureplat.platform.mapper.BasicAuthorizationRecordMapper;
import com.iwomy.secureplat.platform.mapper.BasicOperatorTypeMapper;
import com.iwomy.secureplat.platform.service.BasicOperatorTypeService;
import com.iwomy.secureplat.platform.utils.RemoteDeptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 运营商表
 *
 * @author pig
 * @date 2023-11-13 18:04:59
 */
@Service
public class BasicOperatorTypeServiceImpl extends ServiceImpl<BasicOperatorTypeMapper, BasicOperatorTypeEntity> implements BasicOperatorTypeService {

	@Autowired
	private RemoteDeptUtils remoteDeptUtils;

	@Autowired
	private BasicAuthorizationRecordMapper authorizationRecord;

	@Override
	public Page getBasicOperatorTypePage(Page page, BasicOperatorTypeDTO basicOperatorTypeDTO) {
		LambdaQueryWrapper<BasicOperatorTypeEntity> wrapper = Wrappers.lambdaQuery();

		if (StrUtil.isNotEmpty(basicOperatorTypeDTO.getName())) {
			wrapper.like(BasicOperatorTypeEntity::getName, basicOperatorTypeDTO.getName());
		}

		if (ArrayUtil.isNotEmpty(basicOperatorTypeDTO.getCreateTime())) {
			wrapper.ge(BasicOperatorTypeEntity::getCreateTime, basicOperatorTypeDTO.getCreateTime()[0]).le(BasicOperatorTypeEntity::getCreateTime,
					basicOperatorTypeDTO.getCreateTime()[1]);
		}

		return baseMapper.selectPage(page, wrapper);
	}

	@Override
	public List<? extends Object> pageByScope(BasicOperatorTypeEntity basicOperatorTypeEntity) {
		Long parentId = remoteDeptUtils.getParentId(SecurityUtils.getUser().getDeptId());
		List<BasicOperatorTypeEntity> operatorTypeEntities = new ArrayList<>();
		//是租户时，产品库为设备列表
		if (parentId.equals(0L)) {
			LambdaQueryWrapper<BasicOperatorTypeEntity> queryWrapper = Wrappers.lambdaQuery();
			queryWrapper.eq(BasicOperatorTypeEntity::getState, 10);
			operatorTypeEntities = this.getBaseMapper().selectList(queryWrapper);
		}

		LambdaQueryWrapper<BasicAuthorizationRecordEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(BasicAuthorizationRecordEntity::getDeptId, SecurityUtils.getUser().getDeptId());
		List<BasicAuthorizationRecordEntity> authorizationRecordList = authorizationRecord.selectList(queryWrapper);
		List<Long> operatorTypeIds = authorizationRecordList.stream().map(BasicAuthorizationRecordEntity::getOperatorId).collect(Collectors.toList());
		List<BasicOperatorTypeEntity> resultList = new ArrayList<>();
		if (operatorTypeIds.size() != 0) {
			//多表查询
			MPJLambdaWrapper<BasicOperatorTypeEntity> wrapper = new MPJLambdaWrapper<>();
			wrapper.selectAll(BasicOperatorTypeEntity.class)
					.eq(BasicOperatorTypeEntity::getState, 10)
					.in(BasicOperatorTypeEntity::getId, operatorTypeIds);

			List<BasicOperatorTypeEntity> records = this.getBaseMapper().selectJoinList(BasicOperatorTypeEntity.class, wrapper);

			// 设备类型去重，并转换为BasicOperatorTypeEntity的对象
			Map<Long, BasicOperatorTypeEntity> operatorTypeMap = new HashMap<>();
			Set<Long> communicationModels = new HashSet<>(); // 用于存储已经出现过的 operatorTypeId

			for (BasicOperatorTypeEntity record : records) {
				Long operatorTypeId = record.getId();
				if (!communicationModels.contains(operatorTypeId)) {
					operatorTypeMap.put(operatorTypeId, record);
					communicationModels.add(operatorTypeId); // 将 operatorTypeId 添加到 Set 中
				}
			}
			// 获取去重后的结果列表
			resultList = new ArrayList<>(operatorTypeMap.values());

		}
		if (resultList.isEmpty()) {
			return operatorTypeEntities;
		}

		List<Object> combinedList = Stream.of(operatorTypeEntities, resultList)
				.flatMap(Collection::stream)
				.collect(Collectors.toList());

		return combinedList;
	}
}